package top.superflow.lang

import java.lang.reflect.ParameterizedType
import java.util
import java.util.Collection

import scala.collection.mutable.Traversable
import scala.collection.{IterableLike, mutable}


object CollectionUtils {
  
  def isEmpty(iterable : Iterable[_]) : Boolean = {
    iterable == null ||
      iterable.isEmpty
  }

  def isNotEmpty(iterable : Iterable[_]) : Boolean = {
    return ! isEmpty(iterable)
  }
  
  def isEmpty(collection : Collection[_]) : Boolean = {
    collection == null ||
      collection.isEmpty()
  }

  def isNotEmpty(collection : Collection[_]) : Boolean = {
    return ! isEmpty(collection)
  }


  private val predefinedCollectionClass = Set[Class[_]](classOf[Map[_,_]], classOf[Traversable[_]], classOf[mutable.ListBuffer[_]],classOf[List[_]], classOf[util.Map[_,_]], classOf[util.Collection[_]])
  def isCollection(clz : Class[_]) : Boolean = {
    if(clz == null){
      return false
    }

    var isCollection = false
    for(clt <- predefinedCollectionClass){
      if(clt.isAssignableFrom(clz)){
        isCollection = true
      }
    }

    if(clz.isArray){
      isCollection = true
    }

    return isCollection
  }


  def main(args: Array[String]): Unit = {
    val as = classOf[util.ArrayList[String]].getGenericSuperclass

    if(as.isInstanceOf[ParameterizedType]){
      print(as.asInstanceOf[ParameterizedType].getActualTypeArguments.apply(0))
    }

  }
}